package com.bocloud.cmp.driver.convertor.os;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.openstack4j.model.telemetry.Alarm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.convertor.BeanConvert;
import com.bocloud.cmp.model.AlarmModel;
import com.google.common.collect.Lists;

public class ConvertAlarm implements BeanConvert<AlarmModel, Alarm> {
	private static Logger logger = LoggerFactory.getLogger(ConvertAlarm.class);

	@Override
	public List<AlarmModel> convertList(List<? extends Alarm> models) {
		List<AlarmModel> alarmModels = Lists.newArrayList();
		for (Alarm alarm : models) {
			alarmModels.add(convertModel(alarm));
		}
		return alarmModels;
	}

	@Override
	public AlarmModel convertModel(Alarm alarm) {
		if (null == alarm) {
			return null;
		}
		AlarmModel model = new AlarmModel();
		model.setName(alarm.getName());
		model.setType(alarm.getType().toString());
		model.setRemark(alarm.getDescription());
		model.setGmtTrigger(StringToDate(alarm.getTimestamp()));
		model.setGmtConfirm(StringToDate(alarm.getStateTimestamp()));
		model.setLevel(alarm.getState());
		model.setAlarmVal(alarm.getAlarmId());
		model.setConfirmer(alarm.getUserId());
		model.setGmtConfirm(StringToDate(alarm.getTimestamp()));
		model.setTarget(alarm.getDescription());

		return model;
	}

	private Date StringToDate(String timetamp) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date datetriger = fmt.parse(timetamp.replace("T", " "));
			return datetriger;
		} catch (ParseException e) {
			logger.error("String to Date Error {}", e);
		}
		return null;
	}
}
